<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css">
    <style>
        .layui-table-cell {
            height: 60px;
            line-height: 60px;
            /* height: auto !important;
        white-space: normal;
        padding: 0px; */
        }
        
        th .layui-table-cell {
            height: 30px;
            line-height: 30px;
        }
        
        th .laytable-cell-1-0-7 {
            height: 30px;
            line-height: 30px;
        }
        
        .layui-table img {
            width: 100px;
            height: 60px;
        }
    </style>
</head>

<body>

    <!-- <fieldset class="table-search-fieldset">
        <legend>搜索信息</legend>
        <div style="margin: 10px 10px 10px 10px">
            <form class="layui-form layui-form-pane" action="http://localhost:8080/comment/pageData">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">本人ID</label>
                        <div class="layui-input-inline">
                            <input type="text" name="user_id" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                </div>
            </form>
        </div>
    </fieldset> -->

    <fieldset class="table-search-fieldset">
        <legend>搜索信息</legend>
        <div style="margin: 10px 10px 10px 10px">
            <form class="layui-form layui-form-pane" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户ID</label>
                        <div class="layui-input-inline">
                            <input type="text" name="user_id" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                            lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                    </div>
                </div>
            </form>
        </div>
    </fieldset>


    <table class="layui-hide" id="test" lay-filter="test"></table>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
            <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
            <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
        </div>
    </script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="bookImg">
        <!-- 这里的 checked 的状态只是演示 -->
        <img src="{{d.img}}" name="img" alt="">
    </script>


    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

    <script>
        layui.use(['form', 'table'], function() {
            var table = layui.table,
                form = layui.form,
                $ = layui.jquery;

            table.render({
                elem: '#test',
                url: 'http://localhost:8080/comment/pageData' /*tpa=https://www.layuiweb.com/test/table/demo1.json*/ ,
                toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                    ,
                defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],

                title: '用户数据表',
                contentType: "application/x-www-form-urlencoded",
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'id', title: 'ID', width: 80, sort: true},
                        {field: 'user_id', title: '用户ID', minWidth: 120, edit: 'text'},
                        {field: 'content', title: '评价内容', minWidth: 150, edit: 'text'},
                        {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
                    ]
                ],
                page: true
            });

            //头工具栏事件
            table.on('toolbar(test)', function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：' + data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选' : '未全选');
                        break;
                    case 'add':
                        layer.open({
                            title: '添加评论',
                            shade: 0.2,
                            type: 2,
                            shadeClose: true, // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                            area: ['80%', '100%'],
                            fixed: true, //不固定
                            maxmin: true,
                            content: './add.html',
                            end: function(index) {
                                table.reload('test');
                            }
                        });
                        break;
                    case 'delete':
                        layer.msg('delete');
                        break;
                        //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function(data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {
                    title: '最终的搜索信息'
                });

                //执行搜索重载
                table.reload('test', {
                    page: {
                        curr: 1
                    },
                    where: {
                        //searchParams: result
                        "user_id": data.field.user_id,
                        
                    }
                }, 'data');

                return false;
            });

            //监听行工具事件
            table.on('tool(test)', function(obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function(index) {
                        $.ajax({
                            "url": "http://localhost:8080/comment/delete",
                            "data": {
                                "id": data.id
                            },
                            "success": function(result) {
                                if (result.code == 200) {

                                    layer.msg("删除成功");
                                    obj.del();
                                } else {
                                    layer.msg("删除失败");
                                }
                            }
                        });

                        layer.close(index);
                    });

                } else if (obj.event === 'edit') {
                    // layer.prompt({
                    //   formType: 2
                    //   ,value: data.email
                    // }, function(value, index){
                    //   obj.update({
                    //     email: value
                    //   });
                    //   layer.close(index);
                    // });

                    localStorage.setItem("user", JSON.stringify(data))

                    layer.open({
                        title: '编辑评价',
                        shade: 0.2,
                        type: 2,
                        shadeClose: true,
                        area: ['80%', '100%'],
                        fixed: true, //不固定
                        maxmin: true,
                        content: './commentedit.html',
                        end: function(index) {
                            table.reload('test');
                        }
                    })
                }
            });

        });
    </script>

</body>

</html>